package com.example.tellwin.mine.adapter;

import android.content.Context;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tellwin.R;
import com.example.tellwin.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : LinZePeng
 * @date : 2020/11/23
 * @Describe ：
 **/
public class OrderListPagerAdpater extends RecyclerView.Adapter<OrderListPagerAdpater.OrderListPagerViewHolder> {

    Context context;
    List<OrderListAdapter> listAdapters = new ArrayList<>();
    ItemScrollBottomListener itemScrollBottomListener;

    public void setListAdapters(List<OrderListAdapter> listAdapters) {
        this.listAdapters = listAdapters;
        notifyDataSetChanged();
    }

    public void setItemScrollBottomListener(ItemScrollBottomListener itemScrollBottomListener) {
        this.itemScrollBottomListener = itemScrollBottomListener;
    }

    @NonNull
    @Override
    public OrderListPagerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new OrderListPagerViewHolder(LayoutInflater.from(context).inflate(R.layout.item_order_list_pager ,parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull OrderListPagerViewHolder holder, int position) {
        holder.itemRv.setAdapter(listAdapters.get(position));
        holder.itemRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if(CommonUtils.isSlideToBottom(holder.itemRv)) {
                    if(itemScrollBottomListener!=null){
                        itemScrollBottomListener.onItemScrollBottom(position);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return listAdapters.size();
    }

    class OrderListPagerViewHolder extends RecyclerView.ViewHolder{

        RecyclerView itemRv;

        public OrderListPagerViewHolder(@NonNull View itemView) {
            super(itemView);
            itemRv = itemView.findViewById(R.id.item_rv);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            itemRv.setLayoutManager(linearLayoutManager);
            itemRv.addItemDecoration(new RecyclerView.ItemDecoration() {
                @Override
                public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                    int position = parent.getChildAdapterPosition(view);
                    if(position==0){
                        outRect.top = context.getResources().getDimensionPixelOffset(R.dimen.dp_20);
                    }
                }
            });

        }
    }

    public interface ItemScrollBottomListener{
        void onItemScrollBottom(int position);
    }
}
